package com.sq.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sq.common.enums.ReqTypeEnum;
import com.sq.common.exception.param.ParamNotComplete;
import com.sq.common.exception.param.RepeatDataException;
import com.sq.common.utils.ParamUtil;
import com.sq.common.utils.StringUtils;
import com.sq.system.mapper.SysRoleMapper;
import com.sq.system.pojo.SysRole;
import com.sq.system.service.SysRoleService;
import com.sq.system.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 *
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
    implements SysRoleService
{

    @Autowired
    private SysRoleMapper sysRoleMapper;


    @Override
    public List<SysRole> selectRoleByUserId(String userId) {

        if (StringUtils.isBlank(userId)){
            throw new ParamNotComplete("userId is null");
        }

        List<SysRole> sysRoles = sysRoleMapper.selectRoleByUserId(userId);

        return sysRoles;
    }


    @Override
    public int insertRole(SysRole sysRole) {
        // 判断参数
        ParamUtil.checkParam(sysRole.getClass(), ReqTypeEnum.insert);
        SysRole s = selectRoleByRoleName(sysRole.getRoleName());
        if (!Objects.isNull(s)){
            throw new RepeatDataException(sysRole.getRoleName() + "角色已存在");
        }
        String idStr = IdWorker.getIdStr();
        sysRole.setRoleId(idStr);
        sysRole.setCreateTime(new Date());
        sysRole.setCreateBy(UserInfoUtil.getUserName());
        boolean save = save(sysRole);
        if (save){
            return 1;
        }
        return 0;
    }

    @Override
    public boolean updatRole(SysRole sysRole) {
        ParamUtil.checkParam(sysRole.getClass(),ReqTypeEnum.update);
        SysRole s = selectRoleByRoleName(sysRole.getRoleName());
        if (!Objects.isNull(s)){
            throw new RepeatDataException(sysRole.getRoleName() + "角色已存在");
        }
        sysRole.setUpdateTime(new Date());
        sysRole.setUpdateBy(UserInfoUtil.getUserName());
        return updateById(sysRole);
    }

    @Override
    public SysRole selectRoleByRoleName(String roleName) {
        if (StringUtils.isBlank(roleName)){
            throw new ParamNotComplete("角色名称不能为空");
        }
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name",roleName);
        SysRole sysRole = sysRoleMapper.selectOne(queryWrapper);
        return sysRole;
    }


    @Override
    public List<SysRole> selectRoleWhere(SysRole sysRole) {
        List<SysRole> sysRoles = sysRoleMapper.selectRoleWhere(sysRole);
        return sysRoles;
    }

    @Override
    public int updateRoleStatus(SysRole role) {
        return sysRoleMapper.updateRoleStatus(role);
    }
}




